package net.begincode.servlet.home;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;
import utils.CacheUtils;

import com.google.gson.Gson;

import net.begincode.bean.BcBlog;
import net.begincode.bean.BcCode;
import net.begincode.bean.BcCourse;
import net.begincode.bean.BcDown;
import net.begincode.bean.BcUser;
import net.begincode.bean.CourseChapter;
import net.begincode.bean.ProjectStage;
import net.begincode.servlet.common.BaseServlet;
import net.sf.ehcache.Element;

public class HomeServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(HomeServlet.class);
	
	/**
	 * 进入首页
	 * */
	public void initBeginCode(HttpServletRequest request, HttpServletResponse response){
		try {
			this.homePageCache(request);
			request.getRequestDispatcher("/WEB-INF/page/index.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void homePageCache(HttpServletRequest request){
		//获取最新数据库基础的10章节
//		request.setAttribute("mysqlchapters", this.chapterService.selNewChapterLimitN(this.MYSQLDATABASE,7));
		this.jwebCache(request);
		this.dbCache(request);
		this.coursesCache(request);
		this.codeCache(request);
		this.toolsCache(request);
		this.frameCache(request);
		this.blogCache(request);
		this.blogsCache(request);
		this.projsCache(request);
		
	}
	public void jwebCache(HttpServletRequest request){
		//获取最新java的10章节
		Object jwebEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "jwebchapters");
		if(jwebEle != null){
			request.setAttribute("jwebchapters",(List<CourseChapter>)jwebEle);
		}else{
			List<CourseChapter> ccs = this.chapterService.selNewChapterLimitN(this.JAVA_SOURSE,7);
			request.setAttribute("jwebchapters", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "jwebchapters", ccs);
		}

	}
	//获取最新数据库基础的10章节
	public void dbCache(HttpServletRequest request){
		Object dbEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "dbchapters");
		if(dbEle != null){
			request.setAttribute("dbchapters",(List<CourseChapter>)dbEle);
		}else{
			List<CourseChapter> ccs = this.chapterService.selNewChapterLimitN(this.DATA_BASE,7);
			request.setAttribute("dbchapters", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "dbchapters", ccs);
		}

	}
	//推荐10课程
	public void coursesCache(HttpServletRequest request){
		Object courseEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "courses");
		if(courseEle != null){
			request.setAttribute("courses",(List<BcCourse>)courseEle);
		}else{
			List<BcCourse> ccs = this.courseService.selNewCourseLimitN(7);
			request.setAttribute("courses", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "courses", ccs);
		}
	}
	//最新代码
	public void codeCache(HttpServletRequest request){
		Object codeEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "codes");
		if(codeEle != null){
			request.setAttribute("codes",(List<BcCode>)codeEle);
		}else{
			List<BcCode> ccs = this.codeService.selCodeLimitN(7);
			request.setAttribute("codes", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "codes", ccs);
		}
	}
	//最新工具下载
	public void toolsCache(HttpServletRequest request){
		Object codeEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "tools");
		if(codeEle != null){
			request.setAttribute("tools",(List<BcDown>)codeEle);
		}else{
			List<BcDown> ccs = this.bcDownService.selDownLimitN(this.TOOL, 7);
			request.setAttribute("tools", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "tools", ccs);
		}
	}
	//最新框架下载
	public void frameCache(HttpServletRequest request){
		Object frameEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "frames");
		if(frameEle != null){
			request.setAttribute("frames",(List<BcDown>)frameEle);
		}else{
			List<BcDown> ccs = this.bcDownService.selDownLimitN(this.FRAME, 7);
			request.setAttribute("frames", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "frames", ccs);
		}
	}
	//博客秀
	public void blogCache(HttpServletRequest request){
		Object blogEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "blogShow");
		if(blogEle != null){
			request.setAttribute("blogShow",(BcBlog)blogEle);
		}else{
			BcBlog ccs = this.blogService.blogShow();
			request.setAttribute("blogShow", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "blogShow", ccs);
		}
	}
	//最新博文
	public void blogsCache(HttpServletRequest request){
		Object blogEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "blogs");
		if(blogEle != null){
			request.setAttribute("blogs",(List<BcBlog>)blogEle);
		}else{
			List<BcBlog> ccs = this.blogService.selBlogLimitN(7);
			request.setAttribute("blogs", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "blogs", ccs);
		}
	}
	//项目过程
	public void projsCache(HttpServletRequest request){
		Object blogEle = CacheUtils.getEleByNameAndKey(CacheUtils.HOME_PAGE_CACHE, "proStages");
		if(blogEle != null){
			request.setAttribute("proStages",(List<ProjectStage>)blogEle);
		}else{
			List<ProjectStage> ccs = this.projectStageService.selNewProStageLimitN(5);
			request.setAttribute("proStages", ccs);
			CacheUtils.addEleByParam(CacheUtils.HOME_PAGE_CACHE, "proStages", ccs);
		}
	}
}
